{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里以贵州茅台股票为例，说明pandas_datareader库中股票数据的获取方法及简单的可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Open</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Adj Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-08-01</th>\n",
       "      <td>977.000000</td>\n",
       "      <td>953.020020</td>\n",
       "      <td>976.51001</td>\n",
       "      <td>959.299988</td>\n",
       "      <td>3508952</td>\n",
       "      <td>948.223633</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-02</th>\n",
       "      <td>957.979980</td>\n",
       "      <td>943.000000</td>\n",
       "      <td>944.00000</td>\n",
       "      <td>954.450012</td>\n",
       "      <td>3971940</td>\n",
       "      <td>943.429626</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-05</th>\n",
       "      <td>954.000000</td>\n",
       "      <td>940.000000</td>\n",
       "      <td>945.00000</td>\n",
       "      <td>942.429993</td>\n",
       "      <td>3677431</td>\n",
       "      <td>931.548401</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-06</th>\n",
       "      <td>948.000000</td>\n",
       "      <td>923.799988</td>\n",
       "      <td>931.00000</td>\n",
       "      <td>946.299988</td>\n",
       "      <td>4399116</td>\n",
       "      <td>935.373718</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-07</th>\n",
       "      <td>955.530029</td>\n",
       "      <td>945.000000</td>\n",
       "      <td>949.50000</td>\n",
       "      <td>945.000000</td>\n",
       "      <td>2686998</td>\n",
       "      <td>934.088745</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  High         Low       Open       Close   Volume   Adj Close\n",
       "Date                                                                          \n",
       "2019-08-01  977.000000  953.020020  976.51001  959.299988  3508952  948.223633\n",
       "2019-08-02  957.979980  943.000000  944.00000  954.450012  3971940  943.429626\n",
       "2019-08-05  954.000000  940.000000  945.00000  942.429993  3677431  931.548401\n",
       "2019-08-06  948.000000  923.799988  931.00000  946.299988  4399116  935.373718\n",
       "2019-08-07  955.530029  945.000000  949.50000  945.000000  2686998  934.088745"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import pandas_datareader.data as web\n",
    "import datetime as dt\n",
    "data = web.DataReader('600519.ss','yahoo', dt.datetime(2019,8,1),dt.datetime(2019,8,31))\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/youhaolin/miniconda3/envs/demo/lib/python3.7/site-packages/pyecharts/charts/chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)\n",
      "  super().__init__(init_opts=init_opts)\n"
     ]
    },
    {
     "data": {
      "application/javascript": [
       "new Promise(function(resolve, reject) {\n",
       "    var script = document.createElement(\"script\");\n",
       "    script.onload = resolve;\n",
       "    script.onerror = reject;\n",
       "    script.src = \"https://assets.pyecharts.org/assets/echarts.min.js\";\n",
       "    document.head.appendChild(script);\n",
       "}).then(() => {\n",
       "\n",
       "});"
      ],
      "text/plain": [
       "<pyecharts.render.display.Javascript at 0x105c30a50>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kldata=data.values[:,[2,3,1,0]] # 分别对应开盘价、收盘价、最低价和最高价\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.globals import CurrentConfig, NotebookType\n",
    "CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB\n",
    "from pyecharts.charts import Kline\n",
    "\n",
    "kobj = Kline().add_xaxis(data.index.strftime(\"%Y-%m-%d\").tolist()).add_yaxis(\"贵州茅台-日K线图\", kldata.tolist()).set_global_opts(\n",
    "            yaxis_opts=opts.AxisOpts(is_scale=True),\n",
    "            xaxis_opts=opts.AxisOpts(is_scale=True),\n",
    "            title_opts=opts.TitleOpts(title=\"\"))\n",
    "\n",
    "kobj.load_javascript()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<!DOCTYPE html>\n",
       "<html>\n",
       "<head>\n",
       "    <meta charset=\"UTF-8\">\n",
       "</head>\n",
       "<body>\n",
       "        <div id=\"e150772ff9b1416db4c4e0bd6d7bd13e\" class=\"chart-container\" style=\"width:900px; height:500px;\"></div>\n",
       "    <script>\n",
       "        var chart_e150772ff9b1416db4c4e0bd6d7bd13e = echarts.init(\n",
       "            document.getElementById('e150772ff9b1416db4c4e0bd6d7bd13e'), 'white', {renderer: 'canvas'});\n",
       "        var option_e150772ff9b1416db4c4e0bd6d7bd13e = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"candlestick\",\n",
       "            \"name\": \"\\u8d35\\u5dde\\u8305\\u53f0-\\u65e5K\\u7ebf\\u56fe\",\n",
       "            \"data\": [\n",
       "                [\n",
       "                    976.510009765625,\n",
       "                    959.2999877929688,\n",
       "                    953.02001953125,\n",
       "                    977.0\n",
       "                ],\n",
       "                [\n",
       "                    944.0,\n",
       "                    954.4500122070312,\n",
       "                    943.0,\n",
       "                    957.97998046875\n",
       "                ],\n",
       "                [\n",
       "                    945.0,\n",
       "                    942.4299926757812,\n",
       "                    940.0,\n",
       "                    954.0\n",
       "                ],\n",
       "                [\n",
       "                    931.0,\n",
       "                    946.2999877929688,\n",
       "                    923.7999877929688,\n",
       "                    948.0\n",
       "                ],\n",
       "                [\n",
       "                    949.5,\n",
       "                    945.0,\n",
       "                    945.0,\n",
       "                    955.530029296875\n",
       "                ],\n",
       "                [\n",
       "                    953.5,\n",
       "                    971.6799926757812,\n",
       "                    952.0,\n",
       "                    974.0\n",
       "                ],\n",
       "                [\n",
       "                    978.5,\n",
       "                    962.030029296875,\n",
       "                    958.989990234375,\n",
       "                    980.9500122070312\n",
       "                ],\n",
       "                [\n",
       "                    1010.3099975585938,\n",
       "                    1018.6300048828125,\n",
       "                    995.010009765625,\n",
       "                    1020.0999755859375\n",
       "                ],\n",
       "                [\n",
       "                    1016.1599731445312,\n",
       "                    1017.9500122070312,\n",
       "                    1010.6799926757812,\n",
       "                    1026.0\n",
       "                ],\n",
       "                [\n",
       "                    1030.02001953125,\n",
       "                    1043.3299560546875,\n",
       "                    1030.02001953125,\n",
       "                    1051.9000244140625\n",
       "                ],\n",
       "                [\n",
       "                    1028.0,\n",
       "                    1044.9000244140625,\n",
       "                    1028.0,\n",
       "                    1046.47998046875\n",
       "                ],\n",
       "                [\n",
       "                    1049.8399658203125,\n",
       "                    1054.5999755859375,\n",
       "                    1041.0,\n",
       "                    1075.5799560546875\n",
       "                ],\n",
       "                [\n",
       "                    1061.0,\n",
       "                    1055.8800048828125,\n",
       "                    1049.050048828125,\n",
       "                    1069.97998046875\n",
       "                ],\n",
       "                [\n",
       "                    1055.0,\n",
       "                    1070.0,\n",
       "                    1050.0,\n",
       "                    1073.6800537109375\n",
       "                ],\n",
       "                [\n",
       "                    1070.0999755859375,\n",
       "                    1066.0,\n",
       "                    1061.6600341796875,\n",
       "                    1076.969970703125\n",
       "                ],\n",
       "                [\n",
       "                    1072.989990234375,\n",
       "                    1104.0,\n",
       "                    1070.0,\n",
       "                    1104.0\n",
       "                ],\n",
       "                [\n",
       "                    1105.0,\n",
       "                    1130.0999755859375,\n",
       "                    1095.6199951171875,\n",
       "                    1133.56005859375\n",
       "                ],\n",
       "                [\n",
       "                    1113.0,\n",
       "                    1102.949951171875,\n",
       "                    1096.6800537109375,\n",
       "                    1128.0\n",
       "                ],\n",
       "                [\n",
       "                    1117.0,\n",
       "                    1109.0,\n",
       "                    1109.0,\n",
       "                    1131.31005859375\n",
       "                ],\n",
       "                [\n",
       "                    1109.0,\n",
       "                    1100.1099853515625,\n",
       "                    1083.010009765625,\n",
       "                    1123.18994140625\n",
       "                ],\n",
       "                [\n",
       "                    1105.0,\n",
       "                    1113.0999755859375,\n",
       "                    1092.5,\n",
       "                    1118.1800537109375\n",
       "                ],\n",
       "                [\n",
       "                    1125.0,\n",
       "                    1142.0,\n",
       "                    1123.0,\n",
       "                    1146.0\n",
       "                ]\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u8d35\\u5dde\\u8305\\u53f0-\\u65e5K\\u7ebf\\u56fe\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"\\u8d35\\u5dde\\u8305\\u53f0-\\u65e5K\\u7ebf\\u56fe\": true\n",
       "            },\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": true,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"data\": [\n",
       "                \"2019-08-01\",\n",
       "                \"2019-08-02\",\n",
       "                \"2019-08-05\",\n",
       "                \"2019-08-06\",\n",
       "                \"2019-08-07\",\n",
       "                \"2019-08-08\",\n",
       "                \"2019-08-09\",\n",
       "                \"2019-08-12\",\n",
       "                \"2019-08-13\",\n",
       "                \"2019-08-14\",\n",
       "                \"2019-08-15\",\n",
       "                \"2019-08-16\",\n",
       "                \"2019-08-19\",\n",
       "                \"2019-08-20\",\n",
       "                \"2019-08-21\",\n",
       "                \"2019-08-22\",\n",
       "                \"2019-08-23\",\n",
       "                \"2019-08-26\",\n",
       "                \"2019-08-27\",\n",
       "                \"2019-08-28\",\n",
       "                \"2019-08-29\",\n",
       "                \"2019-08-30\"\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": true,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "        chart_e150772ff9b1416db4c4e0bd6d7bd13e.setOption(option_e150772ff9b1416db4c4e0bd6d7bd13e);\n",
       "    </script>\n",
       "</body>\n",
       "</html>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x105b6db50>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kobj.render_notebook()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
